package MULE.Model.Logic;

import java.awt.event.ActionEvent;
import MULE.Model.Panel.MenuPanel;

/**
 * Logic module responsible for handling menu button events.
 * 
 */
public class MenuLogic extends MulePresenter {

	public enum ActionCommand {
		ENTER_MENU, EXIT_MENU, SAVE_GAME, QUIT_GAME;
	}

	/**
	 * Creates a new menu button logic module.
	 * 
	 * @param view
	 *            The logic module's associated view.
	 */
	public MenuLogic(MenuPanel view) {
		super(view);
		view.addButtonListener(this);
	}

	/**
	 * Asks the user for game name and saves the game.
	 */
	public void saveGame() {
		// Ask user for save name.
		String gameName = MuleDriver.showInputDialog("Save Game",
				"Enter a name to save game by:\n");
		if (gameName == null) { // TODO check game name
			return;
		}
		// Save the game.
		if (MuleDriver.saveGame(gameName)) {
			MuleDriver.showAlertDialog("Save Game", "Game saved!");
		} else {
			MuleDriver.showAlertDialog("Save Game",
					"Could not save the game...");
		}
	}

	/**
	 * Event handler. Responds to events fired on menu button.
	 * 
	 * @param e
	 *            The event which was fired from the menu button.
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (command.equals(ActionCommand.ENTER_MENU.toString())) {
			// Enter menu.
			this.showView();
			if (MuleDriver.getGameLogic().getCurrentRound() > 0) {
				((MenuPanel) view).enableSaveButton(true);
			} else {
				((MenuPanel) view).enableSaveButton(false);
			}
		} else if (command.equals(ActionCommand.EXIT_MENU.toString())) {
			// Exit menu.
			MuleDriver.getGameLogic().showView();
		} else if (command.equals(ActionCommand.SAVE_GAME.toString())) {
			saveGame();
		} else if (command.equals(ActionCommand.QUIT_GAME.toString())) {
			// Quit the game.
			MuleDriver.exitGame();
		}
	}
}
